package com.oa.modules.approvalsec1.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oa.modules.approvalsec1.entity.Storagelog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * (Storagelog)表数据库访问层
 *
 * @author makejava
 * @since 2020-11-19 10:54:58
 */
@Mapper
public interface StoragelogDao extends BaseMapper<Storagelog> {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Storagelog queryById(Long id);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<Storagelog> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param storagelog 实例对象
     * @return 对象列表
     */
    List<Storagelog> queryAll(Storagelog storagelog);

    /**
     * 新增数据
     *
     * @param storagelog 实例对象
     * @return 影响行数
     */
    int insert(Storagelog storagelog);

    /**
     * 修改数据
     *
     * @param storagelog 实例对象
     * @return 影响行数
     */
    int update(Storagelog storagelog);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Long id);

    /**
     * 查询总条数
     *
     * @param params
     * @return
     */
    @Select("<script>" +
            "SELECT COUNT(1) AS total FROM storagelog " +
            "where 1 = 1 " +
            "<when test='params.operation!=null and params.operation != \"\"'> and storagelog.operation LIKE CONCAT('%',#{params.operation},'%') </when>" +
            "<when test='params.projectName!=null and params.projectName != \"\"'> and storagelog.project_name LIKE CONCAT('%',#{params.projectName},'%') </when>" +
            "<when test='params.startDate!=null and params.endDate!=null'> and DATE_FORMAT(storagelog.create_date,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{params.startDate},'%Y-%m-%d') AND DATE_FORMAT(#{params.endDate},'%Y-%m-%d') </when>" +
            "</script>")
    long findPage(@Param("params") Map<String, Object> params);

    /**
     * 查询分页数据
     *
     * @param page
     * @param params
     * @return
     */
    @Select("<script>" +
            "select " +
            "storagelog.* " +
            "from storagelog " +
            "where 1 = 1 " +
            "<when test='params.operation!=null and params.operation != \"\"'> and storagelog.operation LIKE CONCAT('%',#{params.operation},'%') </when>" +
            "<when test='params.projectName!=null and params.projectName != \"\"'> and storagelog.project_name LIKE CONCAT('%',#{params.projectName},'%') </when>" +
            "<when test='params.startDate!=null and params.endDate!=null'> and DATE_FORMAT(storagelog.create_date,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{params.startDate},'%Y-%m-%d') AND DATE_FORMAT(#{params.endDate},'%Y-%m-%d') </when>" +
            "order by storagelog.create_date desc"+
            "</script>")
    List<Storagelog> findRecords(@Param("page") IPage<Storagelog> page,@Param("params") Map<String, Object> params);

    @Insert("<script>" +
            "insert into storagelog(operation,create_date,project_name) values " +
            "<foreach collection='storagelogs' item='item' index='index' separator=','>" +
            "(#{item.operation},#{item.createDate},#{item.projectName})" +
            "</foreach>"
            +"</script>")
    void insertList(@Param("storagelogs") List<Storagelog> storagelogs);
}
